package com.gmics.unifiq;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.SimpleAdapter;

import com.gmics.android.R;
import com.gmics.unifiq.controller.GooglePlace;
import com.gmics.unifiq.entity.Place;
import com.gmics.unifiq.entity.PlaceDetails;
import com.gmics.unifiq.entity.PlaceList;
import com.google.android.gms.internal.an.b;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class ViewEventMap extends Activity {
	private GoogleMap viewEventMap;
	private final LatLng SINGAPORE = new LatLng(1.368123, 103.798981);
	private final LatLng YISHUN = new LatLng(1.4301694,103.8369485);
	private int locationIcon = R.drawable.map_pin;
	private Marker areaMarker;
	private PlaceDetails locationDetails;
	private ProgressDialog pDialog;
	public static int RESULT_FAIL = 2;
	
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_event_map_layout);
		
		new LoadMap().execute();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	class LoadMap extends AsyncTask<String, String, String> {
		 
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ViewEventMap.this);
            pDialog.setMessage("Loading map...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        
        @Override
		protected String doInBackground(String... args) {
            return null;
        }

        @Override
		protected void onPostExecute(String file_url) {
            pDialog.dismiss();
            runOnUiThread(new Runnable() {
                @Override
				public void run() {
            		Bundle b = getIntent().getExtras();
            		
            		try{
	            		locationDetails = (PlaceDetails) b.getSerializable("locationDetails");
	            		
	            		LatLng area = new LatLng(locationDetails.result.geometry.location.lat, locationDetails.result.geometry.location.lng);
	            		
	            		viewEventMap = ((MapFragment) getFragmentManager().findFragmentById(
	            				R.id.view_event_map)).getMap();
	            		viewEventMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
	            		viewEventMap.moveCamera(CameraUpdateFactory.newLatLngZoom(area, 12f));
	            		
	
	            		areaMarker = viewEventMap.addMarker(new MarkerOptions().position(area)
	            				.title(locationDetails.result.name).snippet(locationDetails.result.formatted_address)
	            				.icon(BitmapDescriptorFactory.fromResource(locationIcon)));
            		}
            		catch (Exception e){
	            		String location = b.getString("location");
	            		String title = null, address = null;
	            		LatLng area = null;
	            		
	            		if (location.equals("NUS")){
	            			area = new LatLng(1.3223561, 103.8189167);
	            			title = "National University of Singapore";
	            			address = "21 Lower Kent Ridge Road 119077";
	            		}
	            		else if (location.equals("NTU")){
	            			area = new LatLng(1.346305, 103.681150);
	            			title = "Nanyang Technological University";
	            			address = "Nanyang Ave 639798";
	            		}
	            		else if (location.equals("SMU")){
	            			area = new LatLng(1.297564, 103.849399);
	            			title = "Singapore Management University";
	            			address = "81 Victoria St  188065";
	            		}
	            		else if (location.equals("SIM")){
	            			area = new LatLng(1.328807, 103.775847);
	            			title = "Singapore Institute of Management";
	            			address = "461 Clementi Rd 599491";
	            		}
	            		
	            		viewEventMap = ((MapFragment) getFragmentManager().findFragmentById(
	            				R.id.view_event_map)).getMap();
	            		viewEventMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
	            		viewEventMap.moveCamera(CameraUpdateFactory.newLatLngZoom(area, 12f));
	            		
	
	            		areaMarker = viewEventMap.addMarker(new MarkerOptions().position(area)
	            				.title(title).snippet(address)
	            				.icon(BitmapDescriptorFactory.fromResource(locationIcon)));
            		}
            		
                }
            });
            
        }

 
    }


	/*@Override
	public void onBackPressed() {
		int result = 0;
		Bundle b = new Bundle();
		Intent plannerScreen = new Intent(this, Planner.class);
		
		if (locationName != null){
			plannerScreen.putExtra("locationName", locationName);
			plannerScreen.putExtra("locationRef", locationRef);
			plannerScreen.putExtras(b);
	    	result = RESULT_OK;
		}
		else{
			result = RESULT_FAIL;
		}
		
		setResult(result, plannerScreen);
		super.onBackPressed();
	}*/


}
